import yaml
import math
from data_processing.logger import logger
import os

TYPE = 'png'
MAX_RATING = 16500
MIN_RATING = 11000
MAX_LEFT = 14.7
MIN_LEFT = 8.2
MAX_RIGHT = 15.0
MIN_RIGHT = 9.0
ARGUMENT = 22.512


def data_pre_process():
    folder_path = './out'
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

    with open('./config.yaml', 'r') as config:
        config_data = yaml.safe_load(config)
    rating = config_data.get('rating', None)
    left = config_data.get('left', None)
    right = config_data.get('right', None)
    output_type = config_data.get('type', 'png')
    if rating is None or not isinstance(rating, int) or rating > MAX_RATING or rating < MIN_RATING:
        logger.error(f'rating数据范围异常:{rating}')
        rating = -1
    else:
        if left is None or not isinstance(left, (int, float)) or left > MAX_LEFT or left < MIN_LEFT:
            left = max(round(math.ceil(rating / (5 * ARGUMENT) - 3) / 10, 1), MIN_LEFT)
            logger.warning(f'左值为空或异常,已将范围左值设置为 {left}')
        if right is None or not isinstance(right, (int, float)) or right > MAX_RIGHT or right < MIN_RIGHT:
            right = min(round(math.ceil(rating / (5 * ARGUMENT) + 3) / 10, 1), MAX_RIGHT)
            logger.warning(f'右值为空或异常,已将范围右值设置为 {right}')
    if output_type != 'json':
        output_type = 'png'
    return left, right, rating, output_type
